import json

from .getPublicData import *
from myApp.models import Collection
from django.db.models import F
def addCollection(userInfo,jobId):
    print(userInfo,jobId)
    colData = Collection.objects.filter(user=userInfo,job_id=jobId)
    if len(colData):
        colData[0].count = F('count') + 1
        colData[0].save()
    else:
        Collection.objects.create(user=userInfo,job_id=jobId,count=1)

def getCollectionData(userInfo):
    data = list(Collection.objects.filter(user=userInfo).order_by('-count'))

    def map_fn(item):
        item.job.salary = json.loads(item.job.salary)
        item.job.companyPeople = json.loads(item.job.companyPeople)
        item.job.workTag = json.loads(item.job.workTag)
        if item.job.companyTags != '无':
            item.job.companyTags = json.loads(item.job.companyTags)
        if item.job.internship == '0':
            item.job.salary = list(map(lambda x: str(int(x / 1000)), item.job.salary))
        else:
            item.job.salary = list(map(lambda x: str(x), item.job.salary))
        item.job.salary = '-'.join(item.job.salary)

        item.companyPeople = list(map(lambda x: str(x) + '人', item.job.companyPeople))
        item.companyPeople = '-'.join(item.companyPeople)

        return item
    data = list(map(map_fn, data))
    return data

def removeCollection(colId):
    col = Collection.objects.get(id=colId)
    col.delete()